﻿using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using NpcFramework;
using Neonagee.LocalPreferences;
using OWL.Rendering.HRP;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using Yoozoo.Mars.Got;

namespace Gameplay.PVE
{
    /*
    public static class PveQualityHelper
    {
        public static long PLAYERID = 0;
        
        public static string BLOOM_KEY => "PveQualityHelperBLOOM" + PLAYERID;
        public static string COLOR_AD_KEY => "PveQualityHelperCOLORAD" + PLAYERID;
        public static string SPLITTONING_KEY => "PveQualityHelperSPLITTOING" + PLAYERID;
        public static string SHADOWS_MID_KEY => "PveQualityHelperSHADOWMID" + PLAYERID;
        public static string FOG_KEY => "PveQualityHelperFOG" + PLAYERID;
        public static string LIFT_GAMMA_KEY => "PveQualityHelperLIFTGAMMA" + PLAYERID;

        public static string CASCADE_SHADOW_KEY => "PveQualityHelperCASCADE_SHADOW" + PLAYERID;
        public static string SHADOW_SWITCH_KEY => "PveQualityHelperSHADOW_SWITCH" + PLAYERID;
        public static string CAMERA_FOV_KEY => "PveQualityHelperCAMERA_FOV" + PLAYERID;
        public static string CAMERA_FARCLIP_KEY => "PveQualityHelperCAMERA_FARCLIP" + PLAYERID;
        
        public static string NPC_COUNT_KEY => "PveQualityHelperNPCCOUNT" + PLAYERID;
        public static string CAR_COUNT_KEY => "PveQualityHelperCARCOUNT" + PLAYERID;

        public static string RESOLUTION_MODE_KEY => "PveQualityHelperRESOLUTIONMODE" + PLAYERID;
        
        
        
        // 后效
        public static int openBloom;
        public static int openColor;
        public static int openToning;
        public static int openShadowMid;
        public static int openFog;
        public static int openLiftGamma;

        public static VolumeComponent bloomComponent;
        public static VolumeComponent colorComponent;
        public static VolumeComponent splitComponent;
        public static VolumeComponent shadowComponent;
        public static VolumeComponent fogComponent;
        public static VolumeComponent liftgammaComponent;

        // 相机
        public static int cascadeShadow;
        public static int openShadow;
        public static float cameraFov;
        public static float cameraFarClip;

        // 分辨率
        public static int resolutionMode;
        
        public static Volume Volume;
        public static NpcBirthManager NpcBirthManager;
        public static CarBirthManager CarBirthManager;

        public static Camera Camera;
        public static UniversalAdditionalCameraData AdditionalCameraData;
        public static CinemachineVirtualCameraBase[] VirtualCameraBases;
        
        public static ResolutionSetMode ResolutionSetMode;
        
        // Npc与车辆数量
        public static int npcCount;
        public static int carCount;


        public static void InitPlayerId(long playerId)
        {
            PLAYERID = playerId;
            RevertResolution();
        }

        private static bool resolutionInited = false;
        public static void InitResolutionSetMode(ResolutionSetMode resolutionSetMode)
        {
            if (!resolutionInited)
            {
                resolutionInited = true;   

                ResolutionSetMode = resolutionSetMode;
                RevertResolution();
            }
        }

        public static void RevertResolution()
        {
            if (PLAYERID == 0)
            {
                return;
            }
            
            if (LocalPrefs.HasKey(RESOLUTION_MODE_KEY))
            {
                resolutionMode = LocalPrefs.GetInt(RESOLUTION_MODE_KEY);
                SetResolution(resolutionMode);
            }
            else
            {
                resolutionMode = (int)DeviceQuality.Instance.GetCurrentQuality();
                SetResolution(resolutionMode);
            }

        }

        public static void SetResolution(int rMode)
        {
#if UNITY_ANDROID || UNITY_IOS
            if(ResolutionSetMode==null)
            {
                ResolutionSetMode = new ResolutionSetMode();
                ResolutionSetMode.InitResolutionMode();
                resolutionInited = true;
            }
            ResolutionSetMode.SetResolution((QualityEnum)rMode);
            LocalPrefs.SetInt(RESOLUTION_MODE_KEY,rMode);
#endif
        }

        public static void InitCamera(Camera camera, UniversalAdditionalCameraData additionalCameraData)
        {
            Camera = camera;
            AdditionalCameraData = additionalCameraData;
            RevertCamera();
        }

        public static void RevertCamera()
        {
            if (LocalPrefs.HasKey(CASCADE_SHADOW_KEY))
            {
                cascadeShadow = LocalPrefs.GetInt(CASCADE_SHADOW_KEY);
            }
            else
            {
                cascadeShadow = 1;
            }

            SetCameraCascade(cascadeShadow == 1);

            if (LocalPrefs.HasKey(SHADOW_SWITCH_KEY))
            {
                openShadow = LocalPrefs.GetInt(SHADOW_SWITCH_KEY);
            }
            else
            {
                openShadow = 1;
            }

            SetCameraShadow(openShadow == 1);
        }

        public static void InitVirtualCamera(CinemachineVirtualCameraBase[] virtualCameras)
        {
            VirtualCameraBases = virtualCameras;
            RevertVirtualCamera();
        }

        public static void RevertVirtualCamera()
        {
            if (LocalPrefs.HasKey(CAMERA_FOV_KEY))
            {
                cameraFov = LocalPrefs.GetFloat(CAMERA_FOV_KEY);
                SetCameraFov(cameraFov);

            }

            if (LocalPrefs.HasKey(CAMERA_FARCLIP_KEY))
            {
                cameraFarClip = LocalPrefs.GetFloat(CAMERA_FARCLIP_KEY);
                SetCameraFarClip(cameraFarClip);
            }

        }

        public static void SetCameraFov(float fov)
        {
            if (VirtualCameraBases!=null)
            {
                foreach (var childCamera in VirtualCameraBases)
                {
                    CinemachineVirtualCamera child = childCamera as CinemachineVirtualCamera;
                    if (child)
                    {
                        child.m_Lens.FieldOfView = fov;
                    }
                }
            }
            
            LocalPrefs.SetFloat(CAMERA_FOV_KEY,fov);
        }

        public static void SetCameraFarClip(float farClip)
        {
            if (VirtualCameraBases!=null)
            {
                foreach (var childCamera in VirtualCameraBases)
                {
                    CinemachineVirtualCamera child = childCamera as CinemachineVirtualCamera;
                    if (child)
                    {
                        child.m_Lens.FarClipPlane = farClip;
                    }
                }
            }
            
            LocalPrefs.SetFloat(CAMERA_FARCLIP_KEY,farClip);
        }

        public static void InitVolume(Volume volume)
        {
            Volume = volume;
            Dictionary<Type, VolumeComponent> componentMap = new Dictionary<Type, VolumeComponent>(); 
            foreach (var vc in Volume.sharedProfile.components)
            {
                componentMap[vc.GetType()] = vc;
            }

            bloomComponent = componentMap[typeof(Bloom)];
            colorComponent = componentMap[typeof(ColorAdjustments)];
            splitComponent = componentMap[typeof(SplitToning)];
            shadowComponent = componentMap[typeof(ShadowsMidtonesHighlights)];
            fogComponent = componentMap[typeof(HHeightFog)];        
            liftgammaComponent = componentMap[typeof(LiftGammaGain)];
            
            RevertVolume();
        }

        public static void InitNpcBirthManager(NpcBirthManager birthManager)
        {
            NpcBirthManager = birthManager;
            RevertNpcBirth();
        }

        public static void RevertNpcBirth()
        {
            if (LocalPrefs.HasKey(NPC_COUNT_KEY))
            {
                npcCount = LocalPrefs.GetInt(NPC_COUNT_KEY);
                SetNpcMaxCount(npcCount);
            }
            else
            {
                npcCount = NFConstValue.MaxNpcNum;
                SetNpcMaxCount(npcCount);
            }
        }
        
        public static void InitCarBirthManager(CarBirthManager birthManager)
        {
            CarBirthManager = birthManager;
            RevertCarBirth();
        }

        public static void RevertCarBirth()
        {
            if (LocalPrefs.HasKey(CAR_COUNT_KEY))
            {
                carCount = LocalPrefs.GetInt(CAR_COUNT_KEY);
                SetCarMaxCount(carCount);
            }
            else
            {
                carCount = NFConstValue.MaxCarNum;
                SetCarMaxCount(carCount);
            }
        }
        
        public static void RevertVolume()
        {
            if (LocalPrefs.HasKey(BLOOM_KEY)) {
                openBloom = LocalPrefs.GetInt(BLOOM_KEY);
                SwitchBloom(openBloom == 1);
            }
            else {
                if (bloomComponent) {
                    openBloom = bloomComponent.active ? 1 : 0;
                }
                else {
                    openBloom = 1;
                }
            }

            if (LocalPrefs.HasKey(COLOR_AD_KEY))
            {
                openColor = LocalPrefs.GetInt(COLOR_AD_KEY);
                SwitchColorAdjust(openColor == 1);
            }
            else
            {
                if (colorComponent) {
                    openColor = colorComponent.active ? 1 : 0;
                }
                else {
                    openColor = 1;
                }
            }
            
            if (LocalPrefs.HasKey(SPLITTONING_KEY)) {
                openToning = LocalPrefs.GetInt(SPLITTONING_KEY);
                SwitchSplitToning(openToning == 1);
            }
            else {
                if (splitComponent) {
                    openToning = splitComponent.active ? 1 : 0;
                }
                else {
                    openToning = 1;
                }
            }
            
            if (LocalPrefs.HasKey(SHADOWS_MID_KEY)) {
                openShadowMid = LocalPrefs.GetInt(SHADOWS_MID_KEY);
                SwitchShadowMid(openShadowMid == 1);
            }
            else {
                if (shadowComponent) {
                    openShadowMid = shadowComponent.active ? 1 : 0;
                }
                else {
                    openShadowMid = 1;
                }
            }
            
            if (LocalPrefs.HasKey(FOG_KEY)) {
                openFog = LocalPrefs.GetInt(FOG_KEY);
                SwitchFog(openFog == 1);
            }
            else {
                if (fogComponent) {
                    openFog = fogComponent.active ? 1 : 0;
                }
                else {
                    openFog = 1;
                }
            }
            
            if (LocalPrefs.HasKey(LIFT_GAMMA_KEY)) {
                openLiftGamma = LocalPrefs.GetInt(LIFT_GAMMA_KEY);
                SwitchLiftGamma(openLiftGamma == 1);
            }
            else {
                if (liftgammaComponent) {
                    openLiftGamma = liftgammaComponent.active ? 1 : 0;
                }
                else {
                    openLiftGamma = 1;
                }
            }
        }

        public static void SwitchBloom(bool open)
        {
            if (bloomComponent) {
                bloomComponent.active = open;
                ReOpenVolume();
            }
            LocalPrefs.SetInt(BLOOM_KEY, open ? 1 : 0);
        }
        
        public static void SwitchColorAdjust(bool open)
        {
            if (colorComponent) {
                colorComponent.active = open;
                ReOpenVolume();
            }
            LocalPrefs.SetInt(COLOR_AD_KEY, open ? 1 : 0);
        }
        public static void SwitchSplitToning(bool open)
        {
            if (splitComponent) {
                splitComponent.active = open;
                ReOpenVolume();
            }
            LocalPrefs.SetInt(SPLITTONING_KEY, open ? 1 : 0);
        }
        
        public static void SwitchShadowMid(bool open)
        {
            if (shadowComponent) {
                shadowComponent.active = open;
                ReOpenVolume();
            }
            LocalPrefs.SetInt(SHADOWS_MID_KEY, open ? 1 : 0);
        }
        public static void SwitchFog(bool open)
        {
            if (fogComponent) {
                fogComponent.active = open;
                ReOpenVolume();
            }
            LocalPrefs.SetInt(FOG_KEY, open ? 1 : 0);
        }

        public static void SwitchLiftGamma(bool open)
        {
            if (liftgammaComponent) {
                liftgammaComponent.active = open;
                ReOpenVolume();
            }
            LocalPrefs.SetInt(LIFT_GAMMA_KEY, open ? 1 : 0);
        }

        private static void ReOpenVolume()
        {
            Volume.enabled = false;
            Timers.inst.CallLater(o =>
            {
                Volume.enabled = true;
            });
        }

        public static void SetNpcMaxCount(int count)
        {
            npcCount = count;
            LocalPrefs.SetInt(NPC_COUNT_KEY,count);
            NFConstValue.MaxNpcNum = count;
        }

        public static void SetCarMaxCount(int count)
        {
            carCount = count;
            LocalPrefs.SetInt(CAR_COUNT_KEY,count);
            NFConstValue.MaxCarNum = count;
        }

        
        public static void SetCameraShadow(bool open)
        {
            if (AdditionalCameraData!=null)
            {
                AdditionalCameraData.renderShadows = open;
            }
            LocalPrefs.SetInt(SHADOW_SWITCH_KEY,open?1:0);

        }

        public static void SetCameraCascade(bool open)
        {
            if (AdditionalCameraData!=null)
            {
                if (!open)
                {
                    AdditionalCameraData.m_ShadowCascades = ShadowCascadesOption.NoCascades;
                }
                else
                {
                    AdditionalCameraData.m_ShadowCascades = ShadowCascadesOption.FourCascades;
                }
            }

            LocalPrefs.SetInt(CASCADE_SHADOW_KEY, open ? 1 : 0);
        }

        // 0: full
        // 1: 1/2 
        // 2: 1/4
        // 3: 1/8
        public static void SetTextureQuality(int quality)
        {
            QualitySettings.masterTextureLimit = quality;
        }
        
    }
    */
}